package pre.liuxl.blog.service.serviceImpl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pre.liuxl.blog.DTO.CommentDTO;
import pre.liuxl.blog.entity.UserArticle;
import pre.liuxl.blog.entity.UserComment;
import pre.liuxl.blog.repository.UserArticleRepository;
import pre.liuxl.blog.repository.UserCommentRepository;
import pre.liuxl.blog.repository.UserRepository;
import pre.liuxl.blog.service.UserArticleService;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by liuxuanlong on 2018/6/24.
 */
@Service
public class UserArticleServiceImpl implements UserArticleService {
    @Autowired
    UserArticleRepository userArticleRepository;
    @Autowired
    UserCommentRepository userCommentRepository;
    @Autowired
    UserRepository userRepository;

    @Override
    public void saveUserArticle(Integer userId, String headline, String articleClass, String content) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createDate = simpleDateFormat.format(new Date());
        UserArticle userArticle = new UserArticle();
        userArticle.setUserId(userId);
        userArticle.setHeadline(headline);
        userArticle.setArticleClass(articleClass);
        userArticle.setContent(content);
        userArticle.setCreateDate(createDate);
        userArticleRepository.saveUserArticle(userArticle);
    }

    @Override
    public List<UserArticle> findFirstTenOrderByCreateDate() {
        List<UserArticle> userArticleList = userArticleRepository.findFirstTenOrderByCreateDate();
        return userArticleList;
    }

    @Override
    public List<UserArticle> findFirstFiveOrderByCreateDate() {
        return userArticleRepository.findFirstFiveOrderByCreateDate();
    }

    @Override
    public List<UserArticle> findFirstFiveOrderByPraiseNum() {
        return userArticleRepository.findFirstFiveOrderByPraiseNum();

    }

    @Override
    public UserArticle findById(Integer id) {

        return userArticleRepository.findById(id);
    }

    @Override
    public List<CommentDTO> findByArticleId(Integer articleId) {
        List<CommentDTO> commentDTOList = new ArrayList<>();
        List<UserComment> userCommentList = userCommentRepository.findByArticleId(articleId);
        for (UserComment userComment : userCommentList) {
            CommentDTO commentDTO = new CommentDTO();
            commentDTO.setCreateDate(userComment.getCreateDate());
            commentDTO.setContent(userComment.getContent());
            commentDTO.setNickName(userRepository.findByUserId(userComment.getUserId()).getNickname());
            commentDTOList.add(commentDTO);
        }
        return commentDTOList;
    }

    @Transactional
    @Override
    public void saveComment(Integer articleId, Integer userId, String content) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createDate = simpleDateFormat.format(new Date());
        UserComment userComment = new UserComment();
        userComment.setArticleId(articleId);
        userComment.setUserId(userId);
        userComment.setContent(content);
        userComment.setCreateDate(createDate);
        userCommentRepository.save(userComment);
    }
}
